<template>
	<view class="">
		<nav-bar title="地址管理"></nav-bar>
		<van-address-list @select="selectAddress" v-model="chosenAddressId" :list="list" default-tag-text="默认"
			@add="onAdd" @edit="onEdit(1)">
			<van-icon @click="del" name="cross" color="#ff0000" />
		</van-address-list>
	</view>
</template>

<script>
	import {
		ref
	} from 'vue';
	import storage from '../../../untils/storage';
	const Address = uniCloud.importObject("address")

	export default {
		setup() {
			const chosenAddressId = ref('1');
			const list = [{
					id: '1',
					name: '张三',
					tel: '111111111111',
					address: '河南省信阳市平桥区',
					isDefault: true,
				},
				{
					id: '2',
					name: '李四',
					tel: '0000000000',
					address: '河南省信阳市平桥区',
				},
			];
			return {
				list,
				chosenAddressId,
			};
		},
		data() {
			return {}
		},
		onLoad() {

		},
		methods: {
			onAdd() {
				uni.navigateTo({
					url: "/pages/user/address/add"
				})
			},
			getAddressList() {
				Address.list(storage.userinfo._id).then(res => {
					let list = res.data;
					for (var i = 0; i < list.length; i++) {
						list[i].id = list[i]._id;
						if (list[i].is_default) {
							this.chosenAddressId = list[i]._id
						}
					}
					this.list = list
				})
			},
			onEdit(addr_id) {
				uni.navigateTo({
					url: "/pages/user/address/edit"
				})
			},
			selectAddress(e) {
				this.chosenAddressId = e.id
				Address.setDefault(e.user_id, e.id).then(res => {
					console.log(res);
				})
			}
		}
	};
</script>

<style>
	.van-icon {
		position: absolute;
		right: 40upx;
		top: 100upx;
	}
</style>
